package com.example.statistics.ui.yw

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.yw.MaterialBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.goTimeFormat
import com.example.statistics.ex.onActionLongClicked
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector

/**
 * 原料列表
 */
class MaterialListActivity : BaseLoadingListActivity<MaterialBean>() {

    override val title: String
        get() = "原料"

    override val url: String
        get() = UrlConfig.MATERIAL_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_yw_material

    var action: String? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        action = intent.getStringExtra(Constants.ACTION)
        topBarRightImage {
            activityForResult<MaterialAddActivity>() {
                refresh()
            }
        }
    }


    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: MaterialBean, position: Int) {
        injector.text(R.id.tv_material_name, bean.materialName)
            .text(R.id.tv_specification, "(￥${bean.unitPrice}/KG)")
            .text(R.id.tv_date, "${bean.inventoryNumber}件 | ${bean.inventoryNumber}KG")
            .text(R.id.tv_info, bean.date.goTimeFormat())
            .clicked {
                if (action == Constants.SELECTOR) {
                    activityFinish(Constants.DATA to bean)
                }
            }
            .onActionLongClicked(
                "编辑" to {
                    activityForResult<MaterialEditActivity>(Constants.DATA to bean) {
                        refresh()
                    }
                },
                "删除" to {

                }
            )
    }
}